package com.anuo.app.modules.system.controller;

import com.anuo.app.common.basedata.PayDic;
import com.anuo.app.common.globalhandle.BaseController;
import com.anuo.app.common.entity.Request;
import com.anuo.app.common.globalhandle.GlobalConfig;
import com.anuo.app.common.globalhandle.GlobalLogHandler;
import com.anuo.app.common.thirdparty.SMSUtil;
import com.anuo.app.common.utils.*;
import com.anuo.app.modules.coach.service.CoachService;
import com.anuo.app.modules.study.studyjobschedular.StudyQuartz;
import com.anuo.app.modules.system.entity.*;
import com.anuo.app.modules.system.service.SystemService;
import com.anuo.app.modules.user.entity.UserAccount;
import com.anuo.app.modules.user.entity.UserFilter;
import com.anuo.app.modules.user.service.UserService;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
public class SystemController extends BaseController {
    private static GlobalLogHandler logger = new GlobalLogHandler(SystemController.class);
    @Autowired
    CoachService coachService;
    @Autowired
    SystemService systemService;
    @Autowired
    UserService userService;

    @PostMapping(value = "v1/apiTest", produces = {"application/json;charset=UTF-8"})
    public void apiTest(@RequestBody String reqString, HttpServletRequest request) throws InterruptedException {
        while (true) {
//            Thread.sleep(10);
            String s = "hello";
        }
    }

    @PostMapping(value = "v1/apiGetClientAndServerIp", produces = {"application/json;charset=UTF-8"})
    public String apiGetClientAndServerIp(@RequestBody String reqString, HttpServletRequest request) {
        logger.error("hello");
        String clientIp = request.getRemoteAddr();
        String serverIp = NetUtil.getServerIp();
        return JsonUtil.toJson(clientIp + ";" + serverIp);
    }

    @PostMapping(value = "v1/apiSetMaintenance", produces = {"application/json;charset=UTF-8"})
    public String apiSetMaintenance(@RequestBody String reqString) {
        Maintenance maintenance = JsonUtil.fromJson(reqString, new TypeReference<Request<Maintenance>>() {
        }).getFirstEntity();

        GlobalConfig.inMaintenance = maintenance.isInMaintenance();
        GlobalConfig.maintenanceBeginTime = maintenance.getMaintenanceBeginTime();
        GlobalConfig.maintenanceEndTime = maintenance.getMaintenanceEndTime();
        return rspSuccess();
    }


    @PostMapping(value = "v1/apiQuartzCreate", produces = {"application/json;charset=UTF-8"})
    public String apiQuartzCreate(@RequestBody String reqString) {
        StudyQuartz.deleteJob();
        StudyQuartz.repeatExecute();
        return rspSuccess();
    }

    @PostMapping(value = "v1/apiQuartzResume", produces = {"application/json;charset=UTF-8"})
    public String apiQuartzResume(@RequestBody String reqString) {

        StudyQuartz.resumption();
        return rspSuccess();
    }

    /**
     * 发送验证码
     */
    @PostMapping(value = "v1/apiSendSmsValCode", produces = {"application/json;charset=UTF-8"})
    public String apiSendSmsValCode(@RequestBody String reqString) {
        MobileCode mobileCode = JsonUtil.fromJson(reqString, new TypeReference<Request<MobileCode>>() {
        }).getFirstEntity();
        Assert.isEmpty(mobileCode.getMobile(), "请传手机号");
        int code = SMSUtil.sendSMSCode(mobileCode.getMobile());
        mobileCode.setCode(Integer.toString(code));
        systemService.insertMobileCode(mobileCode);
        return rspSuccess();
    }

    /**
     * 检查验证码
     */
    @PostMapping(value = "v1/apiCheckMobileCode", produces = {"application/json;charset=UTF-8"})
    public String apiCheckMobileCode(@RequestBody String reqString) {
        MobileCode mobileCode = JsonUtil.fromJson(reqString, new TypeReference<Request<MobileCode>>() {
        }).getFirstEntity();
        Assert.isEmpty(mobileCode.getMobile(), "请传手机号");
        Assert.isEmpty(mobileCode.getCode(), "请传验证码");
        systemService.checkMobileCode(mobileCode.getMobile(), mobileCode.getCode());
        return rspSuccess();
    }

    /**
     * 保存极光推送设备id(即每次用户登陆某个设备后, 拿到的此设备极光推送id)
     */
    @PostMapping(value = "v1/apiSaveSystemPushid", produces = {"application/json;charset=UTF-8"})
    public String apiSaveSystemPushid(@RequestBody String reqString) {
        SystemPushid systemPushid = JsonUtil.fromJson(reqString, new TypeReference<Request<SystemPushid>>() {
        }).getFirstEntity();
        Assert.isEmpty(systemPushid.getUserid(), "用户id不能为空");
        Assert.isEmpty(systemPushid.getRegistrationid(), "极光推送的registrationid不能为空");
        systemService.saveSystemPushid(systemPushid);
        return rspSuccess();
    }

    /**
     * 获取最新版本信息
     *
     * @param reqString
     * @return
     */
    @PostMapping(value = "v1/apiGetAppversion", produces = {"application/json;charset=UTF-8"})
    public String apiGetAppversion(@RequestBody String reqString) {
        return rspSuccess(systemService.getAppversion());
    }

    /**
     * 获取账户信息
     *
     * @param reqString
     * @return
     */
    @PostMapping(value = "v1/apiGetAccountInfo", produces = {"application/json;charset=UTF-8"})
    public String apiGetAccountInfo(@RequestBody String reqString) {
        UserFilter userFilter = JsonUtil.fromJson(reqString, new TypeReference<Request<UserFilter>>() {
        }).getFirstEntity();
        //检查数据
        Assert.isEmpty(userFilter.getUserid(),"用户Id不能为空");
        UserAccount userAccount = userService.getUser(userFilter);
        Assert.isEmpty(userAccount,"没有找到此用户");
        if (StringUtil.isNotEmpty(userAccount.getWeixin_unionid())) {
            userAccount.setBind_weixin(true);
        }
        if (StringUtil.isNotEmpty(userAccount.getQq_openid())) {
            userAccount.setBind_qq(true);
        }
        return rspSuccess(userAccount);
    }

    /**
     * 获取城市列表
     *
     * @param reqString
     * @return
     */
    @PostMapping(value = "v1/apiGetCityList", produces = {"application/json;charset=UTF-8"})
    public String apiGetCityList(@RequestBody String reqString) {
        SystemFilter systemFilter = JsonUtil.fromJson(reqString, new TypeReference<Request<SystemFilter>>() {
        }).getFirstEntity();
        //检查数据
        Assert.isEmpty(systemFilter.getCity_name(),"城市名称不能为空");

        List<City> result= systemService.getCityList(systemFilter);
        return rspSuccess(result);
    }

}